package cn.edu.sgu.www.pms.service.impl;

import cn.edu.sgu.www.pms.dto.RolePermissionDTO;
import cn.edu.sgu.www.pms.entity.Permission;
import cn.edu.sgu.www.pms.entity.Role;
import cn.edu.sgu.www.pms.entity.RolePermission;
import cn.edu.sgu.www.pms.exception.GlobalException;
import cn.edu.sgu.www.pms.mapper.PermissionMapper;
import cn.edu.sgu.www.pms.mapper.RoleMapper;
import cn.edu.sgu.www.pms.mapper.RolePermissionMapper;
import cn.edu.sgu.www.pms.pager.Pager;
import cn.edu.sgu.www.pms.pager.impl.PermissionPager;
import cn.edu.sgu.www.pms.restful.ResponseCode;
import cn.edu.sgu.www.pms.service.RolePermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class RolePermissionServiceImpl implements RolePermissionService {

    private final RoleMapper roleMapper;
    private final PermissionMapper permissionMapper;
    private final RolePermissionMapper rolePermissionMapper;

    @Autowired
    public RolePermissionServiceImpl(
            RoleMapper roleMapper,
            PermissionMapper permissionMapper,
            RolePermissionMapper rolePermissionMapper) {
        this.roleMapper = roleMapper;
        this.permissionMapper = permissionMapper;
        this.rolePermissionMapper = rolePermissionMapper;
    }

    @Override
    public void grant(Integer roleId) {
        // 删除角色的权限
        Map<String, Object> columnMap = new HashMap<>(1);

        columnMap.put("role_id", roleId);

        rolePermissionMapper.deleteByMap(columnMap);

        Role role = roleMapper.selectById(roleId);

        // 查询子权限的ID列表
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("id");
        queryWrapper.eq("type", 1);
        queryWrapper.eq("service_id", role.getServiceId());

        List<Permission> permissions = permissionMapper.selectList(queryWrapper);

        // 批量添加角色权限
        RolePermission rolePermission = new RolePermission();

        rolePermission.setRoleId(roleId);

        for (Permission permission : permissions) {
            String permissionId = permission.getId();

            rolePermission.setId(null);
            rolePermission.setPermissionId(permissionId);

            rolePermissionMapper.insert(rolePermission);
        }
    }

    @Override
    public void insert(RolePermissionDTO rolePermissionDTO) {
        Integer roleId = rolePermissionDTO.getRoleId();
        String permissionId = rolePermissionDTO.getPermissionId();

        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("role_id", roleId);
        queryWrapper.eq("permission_id", permissionId);

        Long count = rolePermissionMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new GlobalException(ResponseCode.FORBIDDEN, "角色已经有拥有该权限！");
        }

        RolePermission rolePermission = new RolePermission();

        rolePermission.setRoleId(rolePermissionDTO.getRoleId());
        rolePermission.setPermissionId(rolePermissionDTO.getPermissionId());

        rolePermissionMapper.insert(rolePermission);
    }

    @Override
    public void delete(Integer roleId, List<String > permissionIds) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("role_id", roleId);
        queryWrapper.in(
                !permissionIds.isEmpty(),
                "permission_id", permissionIds
        );

        rolePermissionMapper.delete(queryWrapper);
    }

    @Override
    public Page<Permission> selectByPage(PermissionPager pager) {
        Page<Permission> page = Pager.ofPage(pager);

        return permissionMapper.selectPageByRoleId(page, pager.getRoleId());
    }

}